# This module contains various utility classes used with the jobs scraper and gatherer 
# applications. Each utility tool is modeled as a standalone class. Specifically, it
# contains tools for zipping/unzipping files, creating XML files to dump collected 
# data, database connection, manipulation and access modules, modules for plotting 
# graphs and charts, utilities for generating a random piece of data (numeric/string)
# etc. 

import os, sys, re, time
import GeoIP
import socket
import socks

urlPattern = re.compile(r"^https?:\/\/([^\/]+)/?.*$", re.IGNORECASE)

def getUrlDirPathFromUrl(url):
    urlParts = url.split("/")
    urlPath = "/".join(urlParts[0:-1])
    return (urlPath)


def getCountryNameFromIP(ipaddr):
    gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
    return(gi.country_name_by_addr(ipaddr))


def getIPAddrFromUrl(urlStr):
    urlSearch = urlPattern.search(urlStr)
    if urlSearch:
	hostname = urlSearch.groups()[0]
	return(socket.gethostbyname(hostname))
    else:
	return None


